package cn.kgc.web.controller;

import cn.kgc.common.entity.Article;
import cn.kgc.common.entity.Author;
import cn.kgc.common.entity.Comment;
import cn.kgc.common.entity.DataPage;
import cn.kgc.web.service.ArticleService;
import cn.kgc.web.service.AuthorService;
import cn.kgc.web.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by teacher ZHANG on 2022/11/23
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private AuthorService authorService;

    @RequestMapping("list_comments")
    @ResponseBody
    public DataPage<HashMap<String, Object>> listComments(Integer articleId, Integer pageNum, Integer pageSize) {
        Integer page = pageNum==null? 1 : pageNum;
        Integer size = pageSize==null? 5 : pageSize;

        DataPage<HashMap<String, Object>> dataPage = commentService.listComments(articleId, page, size);

        List<Integer> idList = new ArrayList<>();
        for (HashMap<String, Object> map : dataPage.getDataList()) {
            HashMap<String, Object> author = (HashMap<String, Object>) map.get("author");
            idList.add((Integer) author.get("authorId"));
        }

        System.out.println(idList);

        List<Author> authors = authorService.findAuthor(idList);

        //将nickname赋给map
        for (HashMap<String, Object> map : dataPage.getDataList()) {
            HashMap<String, Object> author = (HashMap<String, Object>) map.get("author");
            Integer authorId = (Integer) author.get("authorId");
            for (Author a : authors) {
                if (authorId.equals(a.getAuthorId())) {
                    author.put("nickname", a.getNickname());
                }
            }
        }

        return dataPage;
    }

/*
    @RequestMapping("save_comment")
    @ResponseBody
    public Map<String, Object> saveComment(Comment comment, @CookieValue(value = "token", required = false) String token) {
        Map<String, Object> result = new HashMap<>();

        System.out.println("token: " + token);

        //token不存在或已过期
        if (token == null || !userService.tokenExist(token)) {
            result.put("success", false);
            result.put("message", "请登录后再发表评论");

            return result;
        }

        Author user = userService.findAuthor(token);

        //当前时间
        comment.setCreateTime(new Date());
        comment.setAuthor(user);

        if (commentService.saveComment(comment) > 0) {
            result.put("success", true);
            result.put("message", "评论保存成功");
        }
        else {
            result.put("success", false);
            result.put("message", "评论保存失败");
        }

        return result;
    }
*/

    @RequestMapping("/list_top")
    @ResponseBody
    public List<Article> listTopComments(Integer number) {
        Integer count = number==null? 5 : number;

        //取评论量最多的文章id
        List<Map<String, Object>> mapList = commentService.listTopComments(count);

        //将文章id存入一个list
        List<Integer> idList = new ArrayList<>();
        for (Map<String, Object> map : mapList) {
            idList.add((Integer) map.get("articleId"));
        }

        //一次查出多篇文章，不要循环多次查询
        List<Article> list = articleService.listByIds(idList);

        //将文章详情与评论量合并
        List<Article> articles = new ArrayList<>();
        for (Map<String, Object> map : mapList) {
            Integer aid = (Integer) map.get("articleId");
            Integer num = (Integer) map.get("commentNum");
            for (Article article : list) {
                if (article.getArticleId().equals(aid)) {
                    article.setCommentNum(num);
                    articles.add(article);
                    break;
                }
            }
        }

        return articles;
    }
}
